package com.liao.seckill.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.liao.seckill.pojo.SeckillMessage;
import com.liao.seckill.pojo.User;
import com.liao.seckill.service.GoodsService;
import com.liao.seckill.service.OrderService;
import com.liao.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */

@Service
@Slf4j
public class MQReceiveMessage {


    @Resource
    private GoodsService goodsService;

    @Resource
    private  OrderService orderService;

    @RabbitListener(queues = "seckillQueue")
    public void queue(String message) {
        log.info("接收到的消息是 -> " + message);
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        User user = seckillMessage.getUser();
        Long goodsId = seckillMessage.getGoodsId();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        orderService.seckill(user,goodsVo);


    }


}
